﻿
//图片上传预览    IE是用了滤镜。
function previewImage(file,index) {
    var allowExtention = ".jpg,.bmp,.gif,.png";
    var extention = file.value.substring(file.value.lastIndexOf(".") + 1).toLowerCase();
    if (allowExtention.indexOf(extention) < 0) {
        alert("仅支持" + allowExtention + "为后缀名的文件!");
        file.value = "";//清空选中文件
        if (browserVersion.indexOf("MSIE") > -1) {
            file.select();
            document.selection.clear();
        }
        file.outerHTML = file.outerHTML;
        return;
    }
    var MAXWIDTH = 260;
    var MAXHEIGHT = 180;
    var div = document.getElementById('preview' + index);
    if (file.files && file.files[0]) {
        div.innerHTML = '<img id=imghead'+index+'>';
        var img = document.getElementById('imghead' + index);
        img.onload = function () {
            var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
            img.width = rect.width;
            img.height = rect.height;
            //img.style.marginLeft = rect.left+'px';
            img.style.marginTop = rect.top + 'px';
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            if (evt.target.result != null)
                img.src = evt.target.result;
        }
        reader.readAsDataURL(file.files[0]);
    }
    else //兼容IE
    {
        var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
        file.select();
        var src = document.selection.createRange().text;
        div.innerHTML = '<img id=imghead' + index + '>';
        var img = document.getElementById('imghead'+ index);
        img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
        var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
        status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
        div.innerHTML = "<div id=divhead style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;" + sFilter + src + "\"'></div>";
    }
}
function clacImgZoomParam(maxWidth, maxHeight, width, height) {
    var param = { top: 0, left: 0, width: width, height: height };
    if (width > maxWidth || height > maxHeight) {
        rateWidth = width / maxWidth;
        rateHeight = height / maxHeight;

        if (rateWidth > rateHeight) {
            param.width = maxWidth;
            param.height = Math.round(height / rateWidth);
        } else {
            param.width = Math.round(width / rateHeight);
            param.height = maxHeight;
        }
    }

    param.left = Math.round((maxWidth - param.width) / 2);
    param.top = Math.round((maxHeight - param.height) / 2);
    return param;
}

function uploadFiles(fileId) {
    document.getElementById("show" + fileId + "Up").style.display = "none";
    document.getElementById("show" + fileId + "Me").style.display = "block";
    var fd = new FormData();
    fd.append("file", document.getElementById("local" + fileId).files[0]);
    var xhr = new XMLHttpRequest();
    //上传中设置上传的百分比
    xhr.upload.addEventListener("progress", function (evt) {
        if (evt.lengthComputable) {
            var percentComplete = Math.round(evt.loaded * 100 / evt.total);
            document.getElementById("show" + fileId + "Me").innerHTML = '上传中' + percentComplete + "%";
        } else {
            document.getElementById("show" + fileId + "Me").innerHTML = '无法计算';
        }
    }, false);
    //请求完成后执行的操作
    xhr.addEventListener("load", function (evt) {
        var message = evt.target.responseText,
            obj = eval("(" + message + ")");
        if (obj.status == 1) {
            $("#" + fileId).val(obj.picName);
            document.getElementById("show" + fileId + "Me").innerHTML = "已上传";
            alert("上传成功!");
        } else {
            alert(obj.message);
        }

    }, false);
    //请求error
    xhr.addEventListener("error", uploadFailed, false);
    //请求中断
    xhr.addEventListener("abort", uploadCanceled, false);
    //发送请求
    xhr.open("POST", "/manage/file/File/uploadPic.htm");
    xhr.send(fd);
}

function uploadFailed(evt) {
    alert("上传出错.");
}

function uploadCanceled(evt) {
    alert("上传已由用户或浏览器取消删除连接.");
}